package edu.gdkm.sql.dao;

import edu.gdkm.sql.pojo.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

    @Select("SELECT * FROM vueshop_user")
    List<User> findAll();

    @Select("SELECT * FROM vueshop_user WHERE id = #{id}")
    @Results(id = "vueshopUserResultMap", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "password", column = "password"),
            @Result(property = "salt", column = "salt"),
            @Result(property = "avatar", column = "avatar")
    })
    User findById(@Param("id") Integer id);

    @Insert("INSERT INTO vueshop_user(username, password, salt, avatar) VALUES(#{username}, #{password}, #{salt}, #{avatar})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(User user);

    @Update("UPDATE vueshop_user SET username=#{username}, password=#{password}, salt=#{salt}, avatar=#{avatar} WHERE id=#{id}")
    void update(User user);

    @Delete("DELETE FROM vueshop_user WHERE id=#{id}")
    void delete(@Param("id") Integer id);
    @Select("SELECT * FROM vueshop_user WHERE username = #{username}")
    User findByUsername(String username);
}
